<template>
  <div class="app-container">

    <div class="tabInfo">
    <el-form
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      v-show="showSearch"
      class="demo-form-inline"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable

          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="deployTime">
        <el-date-picker
          clearable
          v-model="queryParams.deployTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择时间"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  @click="handleQuery">
          <el-icon><Search /></el-icon>搜索
        </el-button>
        <el-button @click="resetQuery">
          <el-icon><Refresh /></el-icon>重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:deployment:remove']"
        >
          <el-icon><Delete /></el-icon>删除
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="todoList1"
            @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="任务编号"
        align="center"
        prop="taskId"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="流程名称" align="center" prop="procDefName" />
      <el-table-column label="当前节点" align="center" prop="taskName" />
      <el-table-column label="流程版本" align="center">
        <template #default="scope">
          <el-tag size="medium">v{{ scope.row.procDefVersion }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="流程发起人" align="center">
        <template #default="scope">
          <label>
            {{ scope.row.startUserName }}
            <el-tag type="info" size="mini">{{ scope.row.startDeptName }}</el-tag>
          </label>
        </template>
      </el-table-column>
      <el-table-column label="接收时间" align="center" prop="createTime" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleProcess(scope.row)"
          >
            处理
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Search, Refresh, Delete } from '@element-plus/icons-vue';
import { todoList, delDeployment } from '@/api/flowable/todo';

const route = useRoute();
const router = useRouter();
const queryFormRef = ref(null);
const loading = ref(true);
const ids = ref([]);
const multiple = ref(true);
const single = ref(true);
const showSearch = ref(true);
const total = ref(0);
const todoList1 = ref([]);
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  name: null,
  category: null,
});

const getList = () => {
  loading.value = true;
  todoList1.value = []; // 初始化列表数据
  todoList(queryParams).then((response) => {
    todoList1.value = response.data.records;
    total.value = response.data.total;
    loading.value = false;
  });
};

const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};

const resetQuery = () => {
  queryParams.name = null;
  queryParams.deployTime = null;
  getList();
};

const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.taskId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
};

const handleProcess = (row) => {
  router.push({
    path: '/flowable/task/todo/detail/index',
    query: {
      procInsId: row.procInsId,
      executionId: row.executionId,
      deployId: row.deployId,
      taskId: row.taskId,
      taskName: row.taskName,
      startUser: `${row.startUserName}-${row.startDeptName}`,
    },
  });
};

const handleDelete = (row) => {
  const idsToDelete = row.taskId || ids.value;
  ElMessageBox.confirm(
    `是否确认删除流程定义编号为"${idsToDelete}"的数据项?`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    delDeployment(idsToDelete).then(() => {
      getList();
      ElMessage.success('删除成功');
    });
  });
};

onMounted(() => {
  getList();
});
</script>

<style>

.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}

</style>
